package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.User;
import com.itheima.service.CheckGroupService;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Reference
    private MenuService menuService;

    @Reference
    private UserService userService;

    //查询当前用户名是否重复
    @RequestMapping("/checkUsername")
    public Boolean checkUsername(@RequestParam("username")String username) {
        return userService.checkUsername(username);
    }

    //获取当前登录用户名及其所属菜单
    @RequestMapping("/getUsernameAndMenu")
    public Result getUsername(Principal principal) {
        try {
            String name = principal.getName();
            Map<String, Object> map = new HashMap<>();
            List menuList = menuService.getMenuList(name);
            map.put("menuList", menuList);
            map.put("username", name);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_MENU_FAIL);
        }
    }


    /**
     * 新增检查组
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestParam("checkItemIds")Integer []checkItemIds,@RequestBody User user){

        try {
            userService.add(checkItemIds,user);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    /**
     * 检查项的分页查询
     */
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            PageResult pageResult = userService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 通过id查询检查组
     */
    @RequestMapping("findById")
    public Result findById(@RequestParam("id")Integer id){

        try {
            User user = userService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);

        }

    }

    /**
     * 编辑更新检查组
     */
    @RequestMapping("edit")
    public Result edit(@RequestParam("checkItemIds")Integer [] checkItemIds,@RequestBody User user){

        try {
            userService.edit(checkItemIds,user);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

    }

    /**
     * 通过id删除检查组
     */

    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id")Integer id){

        try {
            userService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }

    /**
     * 查询所有检查组信息
     */
    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<User> users = userService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,users);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);

        }
    }


}
